<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" @submit.native.prevent>
      <el-form-item label="标题">
        <el-input size="small" v-model="listQuery.q_title"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select
          class="toolbar-lable-right"
          size="small"
          v-model="listQuery.q_isTop"
          placeholder="请选择"
        >
          <el-option
            v-for="item in optStateValues"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="this.getList">查询</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="button-table-margin">
      <el-button size="small" type="primary" @click="append">添加</el-button>
      <el-button size="small" type="primary" @click="edit">修改</el-button>
      <el-button size="small" type="primary" @click="deleteBtn">删除</el-button>
      <el-button size="small" type="primary" @click="toTop">置顶</el-button>
      <el-button size="small" type="primary" @click="unTop">取消置顶</el-button>
    </div>
    <el-table
      ref="singleTable"
      :data="dataList"
      v-loading="listLoading"
      border
      fit
      highlight-current-row
      tooltip-effect="dark"
      style="width: 100%"
      @current-change="handleCurrentChange"
    >
      <el-table-column type="index" align="center" width="50">
      </el-table-column>
      <el-table-column show-overflow-tooltip property="title" label="新闻标题">
      </el-table-column>
      <el-table-column show-overflow-tooltip property="describe" label="描述">
      </el-table-column>
      <el-table-column property="sort" label="排序号">
      </el-table-column>
      <el-table-column label="是否置顶">
        <template slot-scope="scope">
          {{ codeBox.YES_NO[scope.row.isTop] }}
        </template>
      </el-table-column>
      <el-table-column property="createUserName" label="创建人">
      </el-table-column>
      <el-table-column width="160" property="createTime" label="创建时间">
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { getCodeManager, codeToArray } from "@/api/all";
import { noticeListSeller, toTopNoticeSeller, deleteNoticeSeller } from "@/api/seller";
import Pagination from "@/components/Pagination";
import { Message } from "element-ui";
export default {
  name: 'noticelist',
  components: { Pagination },
  data() {
    return {
      dataList: [],
      optStateValues: [],
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        q_title: "",
        q_isTop: "",
      },
      currentRow: null,
    }
  },
  created() {
    // 状态
    getCodeManager("YES_NO", () => {
      this.optStateValues = codeToArray("YES_NO");
    });
    // 列表状态
    getCodeManager("YES_NO", () => {});
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      noticeListSeller(this.listQuery).then(res => {
        this.dataList = res.data.rows;
        this.total = res.data.total;
        this.listLoading = false;
      })
    },
    handleCurrentChange(val) {
      // 选中当前行数据
      this.currentRow = val;
    },

    append() {
      this.$router.push({ path: '/seller/systemnoticeadd' })
    },
    edit() {
      if (!this.currentRow) {
        Message.error("请选择操作行。");
        return;
      }
      this.$router.push({ path: '/seller/systemnoticeedit', query:{id:this.currentRow.id} })
    },
    deleteBtn() {
      if(!this.currentRow) {
        Message.error('请选择操作行。')
        return;
      };
      this.$confirm('确定要删除吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteNoticeSeller({id: this.currentRow.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '删除成功'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        });
      });
    },
    toTop() {
      if(!this.currentRow) {
        Message.error('请选择操作行。')
        return;
      };
      if(this.currentRow.isTop === 1) {
        Message.error('已经是置顶状态，请勿重复操作')
        return;
      }
      this.$confirm('确定要置顶吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        toTopNoticeSeller({id: this.currentRow.id, type: 1}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        });
      });
    },
    unTop() {
      if(!this.currentRow) {
        Message.error('请选择操作行。')
        return;
      };
      if(this.currentRow.isTop === 0) {
        Message.error('已经是取消置顶状态，请勿重复操作')
        return;
      }
      this.$confirm('确定要取消置顶吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        toTopNoticeSeller({id: this.currentRow.id, type: 2}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        });
      });
    },
    reset() {
      this.listQuery.q_title = "";
      this.listQuery.q_isTop = "";
      this.getList();
    },
  }
}
</script>

